// pages/study_task/study_task.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pullDown: false, //下拉刷新页面
    tasks: [{
      task: '老师布置'
    }, {
      task: '自主学习'
    }],
    tupu: [],
    user_id: '',
    kmapping_id: '',
    section_id: 0,
    page: 1,
    initialState: false
  },

  getUserKmappingTask: function(type) {
    var that = this,
      _page = that.data.page,
      pullDown = that.data.pullDown;
    if (!pullDown) {
      wx.showLoading({
        title: 'loading...',
      });
    } else {
      _page = 1
    }
    wx.request({
      url: app.globalData.URL + '/kmapping/WebAppAjax/getUserKmappingTask&' + app.globalData.URLBOOL,
      dataType: 'json',
      method: 'GET',
      data: {
        type: type,
        num: 10,
        page: _page,
        learn_status: 0
      },
      success: function(res) {
        var _res = res.data;
        wx.hideLoading();
        if (_res.error_code == 0) {
          var _tupu = _res.data;
          that.setData({
            'tupu': _tupu,
            'user_id': _tupu[0].user_id,
            'kmapping_id': _tupu[0].kmapping_id,
            'initialState': true
          })
        } else if (_res.error_code == 10002) {
          if (_page == 1) {
            that.setData({
              'tupu': []
            })
          }
          that.setData({
            'initialState': true
          })

        }
        if (pullDown) {
          that.setData({
            pullDown: false,
            page: 1
          })
          wx.hideNavigationBarLoading();
          wx.stopPullDownRefresh();
        }
      }
    })
  },

  clickHandle: function(e) {
    var that = this;
    this.setData({
      //设置active的值为用户点击按钮的索引值
      section_id: e.currentTarget.dataset.index,
      page: 1
    })
    that.getUserKmappingTask(e.currentTarget.dataset.index + 1);
  },

  getCollected: function(index) {
    var that = this,
      tupu = that.data.tupu;
    wx.request({
      url: app.globalData.URL + '/index.php?app=interface&mod=Collection&act=addCollection&' + app.globalData.URLBOOL,
      dataType: 'json',
      method: 'GET',
      data: {
        type: 213,
        id: tupu[index].kmapping_id
      },
      success: function(res) {
        var _res = res.data;
        if (_res.error_code == 0) {
          tupu[index].is_collection = 1;
          that.setData({
            tupu: tupu
          })

          wx.showToast({
            title: '收藏成功',
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: _res.error_msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  getunCollected: function(index) {
    var that = this,
      tupu = that.data.tupu;
    wx.request({
      url: app.globalData.URL + '/index.php?app=interface&mod=Collection&act=deleteCollection&' + app.globalData.URLBOOL,
      dataType: 'json',
      method: 'GET',
      data: {
        type: 213,
        id: tupu[index].kmapping_id
      },
      success: function(res) {
        var _res = res.data;
        if (_res.error_code == 0) {
          tupu[index].is_collection = 0;
          that.setData({
            tupu: tupu
          })
          wx.showToast({
            title: '取消收藏成功',
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: _res.error_msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  collectHandle: function(e) {
    var that = this,
      index = e.currentTarget.dataset.index,
      tupu = that.data.tupu;
    that.getCollected(index);
  },

  uncollectHandle: function(e) {
    var that = this,
      index = e.currentTarget.dataset.index,
      tupu = that.data.tupu;
    that.getunCollected(index);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.getUserKmappingTask(1);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading();
    var that = this,
      section_id = that.data.section_id,
      pullDown = that.data.pullDown;
    that.setData({
      pullDown: true
    })
    if (section_id == 0) {
      that.getUserKmappingTask(1);
    } else {
      that.getUserKmappingTask(2);
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    wx.showLoading({
      title: 'loading',
    })
    var that = this;
    var _page = that.data.page + 1;
    var _type = that.data.section_id + 1;
    wx.request({
      url: app.globalData.URL + '/kmapping/WebAppAjax/getUserKmappingTask&token=' + wx.getStorageSync('token'),
      dataType: 'json',
      method: 'GET',
      data: {
        type: _type,
        num: 10,
        page: _page,
        learn_status: 0
      },
      success: function(res) {
        var _res = res.data;
        wx.hideLoading();
        if (_res.error_code == 0) {
          var _tupu = _res.data;
          var content = that.data.tupu.concat(_tupu);
          that.setData({
            'tupu': content,
            'page': _page
          })
        }
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})